package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.dudu.model.AccountExtend;

import core.CoreDao;
import core.db.DBConnectionUtil;

/**
 * 账号扩展信息数据库操作类
 * 
 * @author 危东
 * @date 2016-4-21
 */
public class AccountExtendDao {

	// 日志输出
	private static final Logger log = Logger.getLogger(AccountExtendDao.class.getSimpleName());

	/**
	 * 插入账号扩展信息表
	 * 
	 * @param coreDao
	 *            数据库连接
	 * @param accountExtend
	 *            输入数据
	 * @return
	 */
	public int insertAccountExtend(CoreDao coreDao, AccountExtend accountExtend) {
		log.info("AccountExtendDao.insertAccountExtend 插入账号扩展信息表");
		// 数据库连接
		Connection con = null;
		// 最终返回结果
		int result = -1;

		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_account_extend(cust_account, account_identify, key_public, key_private, linker_name, linker_phone, linker_email, url_ticket, url_callstatus, url_authentication, url_ivrdigit, url_record,url_sms, billing_platform, create_time, create_user,url_infocontrol) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate(),?,?)");
			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),
					new Object[] { accountExtend.getCust_account(), accountExtend.getAccount_identify(),
							accountExtend.getKey_public(), accountExtend.getKey_private(),
							accountExtend.getLinker_name(), accountExtend.getLinker_phone(),
							accountExtend.getLinker_email(), accountExtend.getUrl_ticket(),
							accountExtend.getUrl_callstatus(), accountExtend.getUrl_authentication(),
							accountExtend.getUrl_ivrdigit(), accountExtend.getUrl_record(), accountExtend.getUrl_sms(),
							accountExtend.getBilling_platform(), accountExtend.getCreate_user(),accountExtend.getUrl_infocontrol() });
			// 获取插入的结果
			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("AccountExtendDao.insertAccountExtend 插入账号扩展信息表出错：" + ex);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}

	/**
	 * 根据custAccount账号标识查询账号信息
	 * 
	 * @param coreDao
	 *            数据库连接
	 * @param custAccount
	 *            custAccount账号标识
	 * @return
	 */
	public AccountExtend queryAccountExtendByCustAccount(CoreDao coreDao, String custAccount) {
		log.info("AccountExtendDao.queryAccountExtendByCustAccount 查询账号扩展信息表");
		// 账号信息类，用于后面返回
		AccountExtend accountExtend = null;
		// 数据库连接
		Connection con = null;

		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写sql
			StringBuffer sql = new StringBuffer("SELECT * FROM t_account_extend WHERE cust_account=?");
			// 查询获取结果
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(), new Object[] { custAccount });
			// 类型转换
			if (list != null && list.size() > 0) {
				// 类型转换
				List<AccountExtend> items = coreDao.covertToBean(list, AccountExtend.class);
				accountExtend = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			log.error("AccountExtendDao.queryAccountExtendByCustAccount 插入账号扩展信息表出错：" + ex);
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return accountExtend;
	}

	/**
	 * 更新账号扩展表
	 * 
	 * @param coreDao
	 *            数据库连接
	 * @param accountExtend
	 *            输入数据
	 * @return
	 */
	public int updateAccountExtend(CoreDao coreDao, AccountExtend accountExtend) {
		log.info("AccountExtendDao.insertAccountExtend 更新账号扩展信息表");
		// 数据库连接
		Connection con = null;
		int result = -1;
		try {
			// 获取数据库连接
			con = DBConnectionUtil.getDBConnection("emate1");
			// 拼写SQL
			StringBuffer sql = new StringBuffer(" UPDATE t_account_extend set cust_account=?,account_identify=?,"
					+ "key_public=?,key_private=?," + "linker_name=?,linker_phone=?,linker_email=?,"
					+ "url_ticket=?,url_callstatus=?,url_authentication=?,url_ivrdigit=?,"
					+ "url_record=?,url_sms=?,billing_platform=?,url_infocontrol=?");
			sql.append(" where id=?");

			// 执行sql，更新相应数据
			List<JSONObject> list = coreDao.serviceJsonSql(con, sql.toString(),
					new Object[] { accountExtend.getCust_account(), accountExtend.getAccount_identify(),
							accountExtend.getKey_public(), accountExtend.getKey_private(),
							accountExtend.getLinker_name(), accountExtend.getLinker_phone(),
							accountExtend.getLinker_email(), accountExtend.getUrl_ticket(),
							accountExtend.getUrl_callstatus(), accountExtend.getUrl_authentication(),
							accountExtend.getUrl_ivrdigit(), accountExtend.getUrl_record(), accountExtend.getUrl_sms(),
							accountExtend.getBilling_platform(), accountExtend.getUrl_infocontrol(),
							accountExtend.getId() });

			if (list != null && list.size() > 0) {
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
}
